package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> list= goodsDao.getListInventory(rows,goodsTypeId,codeOrName,offSet);
        int count= list.size();
        map.put("total",count);
        map.put("rows",list);
        return map;
    }

    //查询所有单位
    @Override
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<Unit> list=goodsDao.list();
        map.put("rows",list);
        return map;
    }

    //查询所有商品信息
    @Override
    public Map<String, Object> goodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> list= goodsDao.goodsList(offSet,rows,goodsName,goodsTypeId);
        int size = list.size();
        map.put("total",size);
        map.put("rows",list);
        return map;
    }

    //添加或修改商品
    @Override
    public ServiceVO saveGoods(Goods goods) {
        int count=0;
        if(goods.getGoodsId()!=null){
            count=goodsDao.update(goods);
        }else {
            count=goodsDao.save(goods);
        }
        if(count>0){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(250,"周傲");
    }

    @Override
    public ServiceVO delete(Integer goodsId) {
        int count=0;
        count=goodsDao.delete(goodsId);
        if(count>0){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(250,"周傲");
    }

    //获取无库存的商品
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> list=goodsDao.getNoInventoryQuantity(offSet,rows,nameOrCode);
        int size = list.size();
        map.put("total",size);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> list=goodsDao.getHasInventoryQuantity(offSet,rows,nameOrCode);
        int size = list.size();
        map.put("total",size);
        map.put("rows",list);
        return map;
    }

    //添加或修改商品期初库存
    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        int count=0;
        if(goodsId!=null){
           count= goodsDao.updateStock(goodsId,inventoryQuantity,purchasingPrice);
        }else {
          count= goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        }
        if(count>0){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(100,"请求成功");
    }

    //删除库存
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        int count=0;
        count=goodsDao.deleteStock(goodsId);
        if(count>0){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(250,"周傲");
    }

    //库存报警
    @Override
    public Map<String, Object> listAlarm() {
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> list=goodsDao.listAlarm();
        map.put("rows",list);
        return map;
    }


}
